﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.Common;
using Spring.Data.Generic;
using System.Reflection;
using System.Data;
using MongoDB.Driver;

namespace Infrastructures
{
    public static class DBHelper
    {
        private static object s_initializeLock = new object();

        public static string GetLastInsertID = "SELECT LAST_INSERT_ID() as id";

        public static MongoDatabase MangoDBInstance { get; private set; }

        public static AdoTemplate TemplateMySQL { get; set; }

        public static void CreateMySqlDBConn(string conn, string dbProvider)
        {
            lock (s_initializeLock)
            {
                if (TemplateMySQL == null)
                {
                    IDbProvider provider = DbProviderFactory.GetDbProvider(dbProvider);
                    provider.ConnectionString = conn;
                    TemplateMySQL = new AdoTemplate(provider);
                }
            }
        }

        public static void CreateMangoDBConn(string conn, string dbName)
        {
            lock (s_initializeLock)
            {
                if (MangoDBInstance == null)
                {
                    MongoClient client = new MongoClient(conn);
                    MongoServer server = client.GetServer();
                    MangoDBInstance = server.GetDatabase(dbName);
                }
            }
        }
    }
}
